package com.sso.managedBean;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.sso.entity.DicHuman;
import com.sso.entity.DicSystem;
import com.sso.service.IDicHumanService;

@ManagedBean(name = "sso_editHumanBean", scope = ManagedBeanScope.REQUEST)
public class EditHumanBean {
	protected IDicHumanService dicHumanService = SpringBeanHolder
			.getBean("dicHumanService");

	@Bind
	@Accessible
	@SaveState
	protected String humanID;

	protected DicSystem system;
	protected DicHuman human;
	// 系统名称
	@Bind(id = "humanName", attribute = "value")
	protected String humanName;
	// 密码
	@Bind(id = "humanPassword", attribute = "value")
	protected String humanPassword;

	// 系统
	@Bind(id = "systemId", attribute = "value")
	protected String systemId;

	@Inject
	private FacesContext context;

	@SaveState
	private String first;

	@BeforeRender
	public void beforeRender(boolean isPostBack) throws Exception {
		if (!isPostBack) {
			humanID = context.getExternalContext().getRequestParameterMap()
					.get("id");
			if (!"".equals(humanID) && null != humanID) {
				human = dicHumanService.getByHumanId(humanID);
				if (system != null) {
					humanID = human.getId();
					humanName = human.getHumanName();
					humanPassword = human.getHumanPassword();
				}
			}
		}
	}

	@Action(id = "save")
	public void save() {
		human = dicHumanService.getByHumanId(humanID);
		human.setId(humanID);
		human.setHumanName(humanName);
		human.setHumanPassword(humanPassword);
		human.setSystem(system);
		dicHumanService.saveDicHuman(human);
		Browser
				.execClientScript("javascript:closeCenterLayoutTab('eidtHumanTab')");
	}
}
